import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '首页', icon: 'House' }
      },
      // 班次管理
      {
        path: 'shift-management',
        name: 'ShiftManagement',
        component: () => import('@/views/ShiftManagement.vue'),
        meta: { title: '班次管理', icon: 'Clock', requiresAuth: true }
      },
      // 排班表管理
      {
        path: 'schedule-management',
        name: 'ScheduleManagement',
        component: () => import('@/views/ScheduleManagement.vue'),
        meta: { title: '排班表管理', icon: 'Calendar', requiresAuth: true }
      },
      // 考勤记录
      {
        path: 'attendance',
        name: 'AttendanceGroup',
        meta: { title: '考勤记录', icon: 'Document' },
        children: [
          {
            path: 'punch-records',
            name: 'PunchRecords',
            component: () => import('@/views/PunchRecords.vue'),
            meta: { title: '原始打卡记录', requiresAuth: true }
          },
          {
            path: 'attendance-records',
            name: 'AttendanceRecords',
            component: () => import('@/views/AttendanceRecords.vue'),
            meta: { title: '考勤记录', requiresAuth: true }
          }
        ]
      },
      // 考勤统计
      {
        path: 'statistics',
        name: 'StatisticsGroup',
        meta: { title: '考勤统计', icon: 'DataAnalysis' },
        children: [
          {
            path: 'monthly-report',
            name: 'MonthlyReport',
            component: () => import('@/views/MonthlyReport.vue'),
            meta: { title: '月度考勤报表', requiresAuth: true }
          },
          {
            path: 'monthly-summary',
            name: 'MonthlySummary',
            component: () => import('@/views/MonthlySummary.vue'),
            meta: { title: '月度考勤汇总', requiresAuth: true, requiresAdmin: true }
          }
        ]
      },
      // 系统管理
      {
        path: 'system',
        name: 'SystemGroup',
        meta: { title: '系统管理', icon: 'Setting', requiresAdmin: true },
        children: [
          {
            path: 'user-management',
            name: 'UserManagement',
            component: () => import('@/views/UserManagement.vue'),
            meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true }
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 考勤管理系统` : '考勤管理系统'
  
  // 如果是登录页面，且已经登录，则跳转到首页
  if (to.path === '/login') {
    if (userStore.isLogin) {
      next('/')
    } else {
      next()
    }
    return
  }
  
  // 需要登录的页面
  if (to.meta.requiresAuth) {
    if (!userStore.isLogin) {
      next('/login')
      return
    }
    
    // 需要管理员权限的页面
    if (to.meta.requiresAdmin && !userStore.isAdmin) {
      ElMessage.error('您没有权限访问此页面')
      next('/')
      return
    }
  }
  
  next()
})

export default router